/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package atm.charts;

import atm.connection.Connect;

import com.alee.laf.panel.WebPanel;

import java.util.ArrayList;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.StandardBarPainter;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.RectangleEdge;

/**
 *
 * @author Quark
 */
public class CategoryBarChartPanel extends WebPanel {

    private Connect connection;
    private ArrayList<Integer> w10;
    private ArrayList<Integer> w20;
    private ArrayList<Integer> w50;
    private ArrayList<Integer> w100;

    public void completeDataset(int i) {
        connection = new Connect();
        connection.getConnected();
        w10 = connection.getCashAmount10(i);
        w20 = connection.getCashAmount20(i);
        w50 = connection.getCashAmount50(i);
        w100 = connection.getCashAmount100(i);
    }

    public CategoryBarChartPanel(int i) {
        completeDataset(i);
        CategoryDataset dataset = createDataset();

        ChartPanel chartPanel = new ChartPanel(createChart(dataset), false);
        chartPanel.setOpaque(false);

        this.add(chartPanel);
    }

    private CategoryDataset createDataset() {
        final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        for (int i = 0; i < w10.size(); i++) {
            dataset.addValue(w10.get(i), "10", getDayString(i));
            dataset.addValue(w20.get(i), "20", getDayString(i));
            dataset.addValue(w50.get(i), "50", getDayString(i));
            dataset.addValue(w100.get(i), "100", getDayString(i));
        }

        return dataset;
    }

    private String getDayString(int i) {
        switch (i) {
            case 0:
                return "M";
            case 1:
                return "T";
            case 2:
                return "W";
            case 3:
                return "TH";
            case 4:
                return "F";
            case 5:
                return "ST";
            case 6:
                return "S";
            default:
                return "Day X";
        }
    }

    private JFreeChart createChart(final CategoryDataset dataset) {
        final JFreeChart chart = ChartFactory.createBarChart(null, null, null, dataset, PlotOrientation.VERTICAL, true, false, false);
        chart.setBackgroundPaint(null);

        chart.getLegend().setBackgroundPaint(null);
        chart.getLegend().setBorder(BlockBorder.NONE);
        chart.getLegend().setPosition(RectangleEdge.TOP);
        //chart.getLegend().setHorizontalAlignment(HorizontalAlignment.RIGHT);

        final CategoryPlot plot = chart.getCategoryPlot();
        plot.setForegroundAlpha(0.7f);
        plot.setBackgroundPaint(null);
        plot.setOutlineVisible(false);
        plot.setDomainGridlinesVisible(false);
        plot.setRangeGridlinesVisible(false);

        // lines from M to S: can be used to draw minima for easier comparision
        /*plot.addAnnotation(new CategoryLineAnnotation("M", 5.0, "S", 5.0, Color.red, new BasicStroke(0.5f)));
         plot.addAnnotation(new CategoryLineAnnotation("M", 8.0, "S", 8.0, Color.green, new BasicStroke(0.5f)));
         plot.addAnnotation(new CategoryLineAnnotation("M", 7.0, "S", 7.0, Color.blue, new BasicStroke(0.5f)));
         plot.addAnnotation(new CategoryLineAnnotation("M", 4.0, "S", 4.0, Color.yellow, new BasicStroke(0.5f)));*/
        
        BarRenderer renderer = new BarRenderer();
        renderer.setDrawBarOutline(false);
        renderer.setShadowVisible(false);
        renderer.setBarPainter(new StandardBarPainter());
        plot.setRenderer(renderer);

        final CategoryAxis domainAxis = plot.getDomainAxis();
        domainAxis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD);
        domainAxis.setLowerMargin(0.0);
        domainAxis.setUpperMargin(0.0);
        domainAxis.setTickMarksVisible(true);
        domainAxis.setMinorTickMarksVisible(true);

        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        rangeAxis.setLabelAngle(0 * Math.PI / 2.0);

        return chart;
    }

}
